﻿namespace ConsoleAppUnion
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            var people1 = new List<Person>
            {
                new Person { Id = 1, Name = "丁一" },
                new Person { Id = 2, Name = "王二" },
            };

            var people2 = new List<Person>
            {
                new Person { Id = 2, Name = "王二1" },
                new Person { Id = 3, Name = "张三" },
            };
            var customComparer = new PersonComparer();
            var unionPeopleCustom = people1.Union(people2, customComparer);

            foreach (var person in unionPeopleCustom)
            {
                Console.WriteLine($"Id: {person.Id}, Name: {person.Name}");
            }
        }
    }

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public override bool Equals(object obj)
        {
            if (obj is Person other)
            {
                return Id == other.Id && Name == other.Name;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(Id, Name);
        }
    }

    public class PersonComparer : IEqualityComparer<Person>
    {
        public bool Equals(Person x, Person y)
        {
            if (x is null && y is null)
                return true;
            if (x is null || y is null)
                return false;
            return x.Id == y.Id;
        }

        public int GetHashCode(Person obj)
        {
            return obj.Id.GetHashCode();
        }
    }
}
